package service

import (
	"net/http"
	"sensor/models"
	Util "sensor/util"

	"github.com/gin-gonic/gin"
)

func ValidToken(c *gin.Context) {
	token := c.PostForm("token")
	jwtToken, err := Util.ParseJwtToken(token)
	if err != nil {
		return
	}
	c.JSON(http.StatusOK, gin.H{
		"msg":  "登录成功",
		"data": jwtToken,
		"code": 1,
	})
}

func Login(c *gin.Context) {
	username := c.PostForm("username")
	password := c.PostForm("password")
	result, msg := models.Login(username, password)
	if !result {
		c.JSON(http.StatusOK, gin.H{
			"msg":  msg,
			"data": "",
			"code": 0,
		})
	} else {
		c.JSON(http.StatusOK, gin.H{
			"msg":  "登录成功",
			"data": msg,
			"code": 1,
		})
	}
}
